package stream;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class UseCollectingAndThen {

    @Test
    public void test01(){
        List<Dish> dishList = new ArrayList<>();
        Dish dish1 = new Dish("001", "张三");
        dishList.add(dish1);
        Dish dish2 = new Dish("001", "李四");
        dishList.add(dish2);
        Dish dish3 = new Dish("002", "王五");
        dishList.add(dish3);
        Dish dish4 = new Dish("003", "阿森纳");
        dishList.add(dish4);

        Map<String, Long> collect = dishList.stream().collect(Collectors.groupingBy(Dish::getId, Collectors.counting()));

        Map<String, Integer> collect2 = dishList.stream().collect(Collectors.groupingBy(Dish::getId, Collectors.collectingAndThen(Collectors.counting(), Long::intValue)));

    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    private static class Dish {
        private String id;
        private String name;
    }

}
